var watch = require('watch');
const { exec } = require('child_process');
const { RESTART_OCTOPODA } = require('./cmd');
process.on('SIGINT', () => {
  watched = false;
  process.exit(0);
});
watchOctopoda();
function restartOctopodaProcess(){
    console.log(RESTART_OCTOPODA);
}
var watched = false;
function watchOctopoda(){
  if (watched) {
    return;
  }
  watch.createMonitor(__dirname + '/../', function (monitor) {
    watched = true;
    watch.unwatchTree(__dirname + '/../dev');
    watch.unwatchTree(__dirname + '/../logs');
    monitor.on('created', function (f, stat) {
      // Handle new files
      if (/[\\/](?:logs|dev|mysql)[\\/]/.test(f)) {
        return;
      }
      restartOctopodaProcess();
    });
    monitor.on('changed', function (f, curr, prev) {
      // Handle file changes
      if (/[\\/](?:logs|dev|mysql)[\\/]/.test(f)) {
        return;
      }
      restartOctopodaProcess();
    });
    monitor.on('removed', function (f, stat) {
      // Handle removed files
      if (/[\\/](?:logs|dev|mysql)[\\/]/.test(f)) {
        return;
      }
      restartOctopodaProcess();
    });
    //monitor.stop(); // Stop watching
  });
}